package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.dao.MenuFoodMaterialDao;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.entity.MenuFoodMaterial;
import com.dlc.felear.xiaoensale.entity.SideDish;
import com.dlc.felear.xiaoensale.entity.WeightInfo;
import com.dlc.felear.xiaoensale.helper.JsonHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


public class SideDishAdapter extends BaseAdapter {

    private static final String TAG = "SideDishAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<SideDish> items;
    private boolean enbale = true;
    private boolean isSetting = false;

    public SideDishAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
    }

    public void initData() {
        items.clear();
        notifyDataSetChanged();
    }

    public List<SideDish> getData() {
        return items;
    }

    public void setWeightInfo(WeightInfo data) {
        try {
            List<SideDish> tempList = items;
            Collections.sort(tempList);
            SideDish tempSideDish = getSideDish(data.getNum());
            if (tempSideDish != null) {
                tempSideDish.setWeight(data.getWeight());
                setItem(tempSideDish);
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    private SideDish getSideDish(int num) {
        for (SideDish s : items) {
            if (num == s.getNum()) {
                return s;
            }
        }
        return null;
    }

    public void editStdWeight(float current) {
        Log.e(TAG, " current=" + current);

        resetStdWeight(current);
    }

    private void resetStdWeight(float value) {
        isSetting = true;
        Log.e(TAG, "resetStdWeight value=" + value);
        for (int i = 0; i < items.size(); i++) {
            SideDish s = items.get(i);
            items.remove(i);
            float temp = 0f;
            if (value > 0) {
                temp = (value * 1000) / s.getTotalWeight() * s.getStdWeight();
            }
            Log.e(TAG, "食材:" + s.getName() + "StdWeight=" + s.getStdWeight() + "换算后的重量:" + temp);
            s.setStdWeight1((int) temp);
            items.add(i, s);
        }
        notifyDataSetChanged();
        isSetting = false;
    }

    public void setItem(SideDish data) {
        try {
            if (data == null) {
                notifyDataSetChanged();
                return;
            }
            SideDish temp = getSideDish(data.getNum());
            Log.e(TAG, "setItem data=" + JsonHelper.toJson(data) + " temp=" + JsonHelper.toJson(temp));
            if (temp == null) {
                items.add(data);
                notifyDataSetChanged();
                return;
            }
            if (isSetting) {
                return;
            }
            items.remove(temp);
            if (data.getDisplay() != temp.getDisplay()) {
                temp.setDisplay(temp.getDisplay());
            }
            temp.setName(data.getName());
            temp.setTotalWeight(data.getTotalWeight());
            temp.setStdWeight1(data.getStdWeight1());
            temp.setWeight(data.getWeight());
            items.add(temp);
            Collections.sort(items);
            notifyDataSetChanged();
        } catch (Exception e) {

        }

    }

    @Override
    public int getCount() {

        return items == null ? 0 : items.size();
    }

    @Override
    public SideDish getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_side_dish, null);
            holder.num = convertView.findViewById(R.id.tv_s_num);
            holder.name = convertView.findViewById(R.id.tv_s_name);
            holder.weight = convertView.findViewById(R.id.tv_s_wight);
            holder.stdWeight = convertView.findViewById(R.id.tv_s_stdWeight);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(items.get(position).getName());
        holder.num.setText(Integer.toString(items.get(position).getNum()));
        holder.weight.setText(Integer.toString(items.get(position).getWeight()));
        holder.stdWeight.setText(Integer.toString(items.get(position).getStdWeight1()));
        setTextColor(holder.weight, items.get(position).getWeight(), items.get(position).getStdWeight());
        return convertView;
    }

    private void setTextColor(TextView tv, int weight, int stdWeight) {
        if (Math.abs(weight - stdWeight) < 50) {
            tv.setTextColor(0xff000000);
        } else {
            tv.setTextColor(0xffff0000);
        }
    }

    public void initWeight(Map<Integer, Integer> data) {
        if (data == null || data.isEmpty()) {
            return;
        }
        for (int i = 0; i < items.size(); i++) {
            SideDish sideDish = items.get(i);
            if (!data.containsKey(sideDish.getNum())) {
                continue;
            }
            int weight = sideDish.getWeight() - data.get(sideDish.getNum());
            if (weight <= 0) {
                weight = 0;
            }
            sideDish.setWeight(weight);
            items.remove(i);
            items.add(i, sideDish);
        }
        notifyDataSetChanged();
    }

    public void setCookMenu(CookingMenu cookingMenu) {
        try {
            initData();
            MenuFoodMaterialDao foodMaterialDao = new MenuFoodMaterialDao(context);
            List<MenuFoodMaterial> list = foodMaterialDao.queryForAllByCId(cookingMenu.getId());
            Log.e(TAG, "name:" + cookingMenu.getName() + " foods:" + JsonHelper.toJson(list));
            if (list == null || list.isEmpty()) {
                Log.e(TAG, "name:" + cookingMenu.getName());
                return;
            }
            Collections.sort(list);
            items.clear();
            for (MenuFoodMaterial foodMaterial : list) {
                SideDish sideDish = new SideDish();
                sideDish.setStdWeight((int) foodMaterial.getWeight());
                sideDish.setStdWeight1((int) foodMaterial.getWeight());
                sideDish.setTotalWeight((int) (Float.parseFloat(cookingMenu.getSpecification()) * 1000));
                sideDish.setName(foodMaterial.getFoodName());
                sideDish.setFoodid(foodMaterial.getFoodid());
                sideDish.setId(foodMaterial.getId());
                sideDish.setNum(foodMaterial.getNumber());
                Log.e(TAG, "sideDish=" + JsonHelper.toJson(sideDish));
                items.add(sideDish);
            }
            Collections.sort(items);
            Log.e(TAG, "items=" + JsonHelper.toJson(items));
            notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    static class ViewHolder {
        TextView name, num, weight, stdWeight;
    }

    private void showLog(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public void setEnbale(boolean flag) {
        this.enbale = flag;
    }

}